﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AppServer.Controllers
{
    [SysPageFilte]
    [SysPageError]
    public class SysControllerBase: ControllerBase
    {
        /// <summary>
        /// 得到Session
        /// </summary>
        /// <param name="key">键</param>
        public Mssystem_1 GetSessionUser()
        {
            string value = GetSession("LoginUser");
            Mssystem_1 ms = Mssystem_1.parse(value);
            ms = new BLLsystem_1().Getsystem_1MsByKey(ms.System_1_10.ToString());
            return ms;
        }


        /// <summary>
        /// 得到Session
        /// </summary>
        /// <param name="key">键</param>
        public T GetSession<T>(string key)
        {
            var value = HttpContext.Session.GetString(key);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;
            JObject _value = JObject.Parse(value);
            T ms = (T)_value.ToObject(typeof(T));
            return ms;
        }
        /// <summary>
        /// 得到Session
        /// </summary>
        /// <param name="key">键</param>
        public string GetSession(string key)
        {
            var value = HttpContext.Session.GetString(key);
            if (string.IsNullOrEmpty(value))
                value = string.Empty;
            return value;
        }
        /// <summary>
        /// 设置Session
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        protected void SetSession(string key, string value)
        {
            HttpContext.Session.SetString(key, value);
        }
        /// <summary>
        /// 设置Session
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        protected void SetSession(string key, object value)
        {
            string _value = JObject.FromObject(value).ToString();
            SetSession(key, _value);
        }
    }
}
